<!-- Copyright (c) Abstract Machines -->
<!-- SPDX-License-Identifier: Apache-2.0 -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{.Title}}</title>
    <style>
        :root {
            --primary-color: rgb(41, 128, 185);
            --secondary-color: rgb(26, 82, 118);
            --subtle-color: rgb(189, 195, 199);
            --table-header-bg: rgb(236, 240, 241);
            --alternate-row: rgb(245, 247, 249);
            --text-primary: rgb(44, 62, 80);
            --text-secondary: rgb(127, 140, 141);
            --white: #ffffff;
            
            --header-height: 35mm;
            --footer-height: 20mm;
            --page-padding: 15mm;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: var(--white);
            color: var(--text-primary);
            line-height: 1.4;
        }

        .page {
            max-width: 210mm;
            min-height: 297mm;
            padding: var(--page-padding) 10mm;
            margin: 5mm auto 0 auto;
            background: var(--white);
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            position: relative;
            display: flex;
            flex-direction: column;
        }

        .header {
            height: var(--header-height);
            min-height: var(--header-height);
            max-height: var(--header-height);
            position: relative;
            flex-shrink: 0;
            display: flex;
            flex-direction: column;
        }

        .header-top-bar {
            height: 8px;
            background-color: var(--primary-color);
            margin: 5px 0 10px 0;
            flex-shrink: 0;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            flex-shrink: 0;
        }

        .header-title {
            font-size: 20px;
            font-weight: bold;
            color: var(--primary-color);
            text-align: center;
            flex-grow: 1;
        }

        .header-date {
            font-size: 10px;
            font-style: italic;
            color: var(--text-secondary);
            text-align: right;
            width: 100px;
        }

        .header-separator {
            height: 2px;
            background-color: var(--subtle-color);
            margin: 5px 0 10px 0;
            position: relative;
            flex-shrink: 0;
        }

        .header-separator::after {
            content: '';
            position: absolute;
            top: 3px;
            left: 0;
            right: 0;
            height: 1px;
            background-color: var(--subtle-color);
        }

        .content-area {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            min-height: 0;
            overflow: hidden;
        }

        .metrics-section {
            margin-bottom: 15px;
            flex-shrink: 0;
        }

        .metrics-title {
            font-size: 16px;
            font-weight: bold;
            color: var(--secondary-color);
            margin-bottom: 10px;
        }

        .metrics-info {
            background-color: var(--alternate-row);
            padding: 12px;
            border-radius: 4px;
            margin-bottom: 10px;
        }

        .metric-row {
            display: flex;
            margin-bottom: 8px;
        }

        .metric-row:last-child {
            margin-bottom: 0;
        }

        .metric-label {
            font-weight: bold;
            color: var(--text-primary);
            width: 120px;
            font-size: 11px;
        }

        .metric-value {
            font-style: italic;
            color: var(--text-primary);
            font-size: 11px;
            flex-grow: 1;
        }

        .record-count {
            text-align: right;
            font-size: 10px;
            font-style: italic;
            color: var(--text-secondary);
            margin-bottom: 10px;
            flex-shrink: 0;
        }

        .table-container {
            flex-grow: 1;
            overflow: auto;
            min-height: 0;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
        }

        .table-header-bar {
            height: 4px;
            background-color: var(--primary-color);
        }

        .data-table th {
            background-color: var(--table-header-bg);
            color: var(--secondary-color);
            font-weight: bold;
            font-size: 11px;
            padding: 8px;
            text-align: center;
            border-bottom: 2px solid var(--subtle-color);
            position: sticky;
            top: 0;
        }

        .data-table td {
            padding: 6px 8px;
            font-size: 10px;
            text-align: center;
            border-bottom: 1px solid #eee;
        }

        .data-table tr:nth-child(even) {
            background-color: var(--alternate-row);
        }

        .data-table tr:hover {
            background-color: rgba(41, 128, 185, 0.05);
        }

        .col-time {
            width: 25%;
            color: var(--text-primary);
        }

        .col-value {
            width: 17%;
            color: var(--text-primary);
            font-weight: normal;
        }

        .col-unit {
            width: 17%;
            color: var(--text-secondary);
            font-style: italic;
        }

        .col-protocol {
            width: 17%;
            color: var(--text-primary);
        }

        .col-subtopic {
            width: 24%;
            color: var(--secondary-color);
        }

        .footer {
            height: var(--footer-height);
            min-height: var(--footer-height);
            max-height: var(--footer-height);
            border-top: 2px solid var(--subtle-color);
            padding-top: 8px;
            flex-shrink: 0;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
        }

        .footer-separator {
            height: 1px;
            background-color: var(--subtle-color);
            margin-bottom: 6px;
            position: relative;
            flex-shrink: 0;
        }

        .footer-separator::after {
            content: '';
            position: absolute;
            top: 1px;
            left: 0;
            right: 0;
            height: 1px;
            background-color: var(--subtle-color);
        }

        .footer-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 0;
            padding: 0;
            flex-shrink: 0;
        }

        .footer-generated {
            font-size: 8px;
            font-style: italic;
            color: var(--text-secondary);
            margin: 0;
            padding: 0;
        }

        .footer-page {
            font-size: 9px;
            font-weight: bold;
            color: var(--text-primary);
            margin: 0;
            padding: 0;
        }

        @media print {
            .page {
                box-shadow: none;
                margin: 0;
                max-width: none;
                height: 297mm;
                min-height: auto;
                page-break-after: always;
            }
            
            .page:last-child {
                page-break-after: auto;
            }
        }
    </style>
</head>
<body>
    {{$totalPages := len .Reports}}
    {{if eq $totalPages 0}}
    {{$totalPages = 1}}
    {{end}}
    {{$globalPage := 0}}
    {{if gt (len .Reports) 0}}
    {{range $index, $report := .Reports}}
    {{$globalPage = add $globalPage 1}}
    <div class="page">
        <div class="header">
            <div class="header-top-bar"></div>
            <div class="header-content">
                <div style="width: 100px;"></div>
                <div class="header-title">{{$.Title}}</div>
                <div class="header-date">{{$.GeneratedDate}}</div>
            </div>
            <div class="header-separator"></div>
        </div>

        <div class="content-area">
            <div class="metrics-section">
                <div class="metrics-title">Metrics</div>
                <div class="metrics-info">
                    <div class="metric-row">
                        <div class="metric-label">Name:</div>
                        <div class="metric-value">{{.Metric.Name}}</div>
                    </div>
                    {{if .Metric.ClientID}}
                    <div class="metric-row">
                        <div class="metric-label">Device ID:</div>
                        <div class="metric-value">{{.Metric.ClientID}}</div>
                    </div>
                    {{end}}
                    <div class="metric-row">
                        <div class="metric-label">Channel ID:</div>
                        <div class="metric-value">{{.Metric.ChannelID}}</div>
                    </div>
                </div>
            </div>

            <div class="record-count">
                Total Records: {{len .Messages}}
            </div>

            <div class="table-container">
                <div class="table-header-bar"></div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th class="col-time">Time</th>
                            <th class="col-value">Value</th>
                            <th class="col-unit">Unit</th>
                            <th class="col-protocol">Protocol</th>
                            <th class="col-subtopic">Subtopic</th>
                        </tr>
                    </thead>
                    <tbody>
                        {{range .Messages}}
                        <tr>
                            <td class="col-time">{{formatTime .Time}}</td>
                            <td class="col-value">{{formatValue .}}</td>
                            <td class="col-unit">{{.Unit}}</td>
                            <td class="col-protocol">{{.Protocol}}</td>
                            <td class="col-subtopic">{{.Subtopic}}</td>
                        </tr>
                        {{end}}
                    </tbody>
                </table>
            </div>
        </div>

        <div class="footer">
            <div class="footer-separator"></div>
            <div class="footer-content">
                <div class="footer-generated">Generated: {{$.GeneratedTime}}</div>
                <div class="footer-page">Page {{$globalPage}} of {{$totalPages}}</div>
            </div>
        </div>
    </div>
    {{end}}
    {{else}}
    {{$globalPage = 1}}
    <div class="page">
        <div class="header">
            <div class="header-top-bar"></div>
            <div class="header-content">
                <div style="width: 100px;"></div>
                <div class="header-title">{{.Title}}</div>
                <div class="header-date">{{.GeneratedDate}}</div>
            </div>
            <div class="header-separator"></div>
        </div>

        <div class="content-area">
            <div class="metrics-section">
                <div class="metrics-title">Metrics</div>
                <div class="metrics-info">
                    <div class="metric-row">
                        <div class="metric-label">Name:</div>
                        <div class="metric-value">{{.Metric.Name}}</div>
                    </div>
                    <div class="metric-row">
                        <div class="metric-label">Channel ID:</div>
                        <div class="metric-value">{{.Metric.ChannelID}}</div>
                    </div>
                </div>
            </div>

            <div class="record-count">
                Total Records: 0
            </div>

            <div class="table-container">
                <div class="table-header-bar"></div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th class="col-time">Time</th>
                            <th class="col-value">Value</th>
                            <th class="col-unit">Unit</th>
                            <th class="col-protocol">Protocol</th>
                            <th class="col-subtopic">Subtopic</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" style="text-align: center; font-style: italic; color: #888;">No data available</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="footer">
            <div class="footer-separator"></div>
            <div class="footer-content">
                <div class="footer-generated">Generated: {{.GeneratedTime}}</div>
                <div class="footer-page">Page {{$globalPage}} of {{$totalPages}}</div>
            </div>
        </div>
    </div>
    {{end}}

</body>
</html>
